package com.mashuai.utils.ztest.toolkit.handleBean;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mashuai.utils.base.MsAssertUtils;
import com.mashuai.utils.toolkit.core.msHandlebean.dto.MsDictDto;
import com.mashuai.utils.toolkit.core.msMp.MsDb;
import com.mashuai.utils.toolkit.inter.IMsGetDict;
import com.mashuai.utils.ztest.pb.bean.MsDict;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: MsGetDictImpl
 * @Description: 实现本项目的字典处理接口
 */
@Component
public class MsGetDictImpl implements IMsGetDict {
    @Override
    public Map<String, List<MsDictDto>> getDictData(List<String> dictTypeList) {
        Map<String, List<MsDictDto>> resultMap = new HashMap<>();
        MsAssertUtils.objIsNull(dictTypeList, "请传入字典类型");
        //一次性查询需要处理的字典值
        LambdaQueryWrapper<MsDict> qw = Wrappers.lambdaQuery();
        qw.in(MsDict::getType, dictTypeList);
        List<MsDict> dictList = MsDb.list(qw, MsDict.class);
        MsAssertUtils.objIsNull(dictList, "未获取到任何字典值");
        //集合按字典类型分组
        Map<String, List<MsDict>> dictMap = dictList.stream().collect(Collectors.groupingBy(t -> t.getType()));
        dictTypeList.forEach(dictType -> {
            List<MsDictDto> addList = new ArrayList<>();
            List<MsDict> msDictList = dictMap.get(dictType);
            if (msDictList != null && msDictList.size() != 0) {
                msDictList.forEach(t -> {
                    //TODO 在这里把自己项目中存放字典的数据，塞入到工具方法所需要的MsDictDto中即可
                    MsDictDto dictDto = new MsDictDto();
                    dictDto.setId(t.getId());
                    dictDto.setType(t.getType());
                    dictDto.setValue(t.getValue());
                    dictDto.setLabel(t.getLabel());
                    addList.add(dictDto);
                });
                if (addList.size() != 0) resultMap.put(dictType, addList);
            }
        });
        return resultMap;
    }
}
